#include "stdafx.h"
#include "ASN1Bool.h"

namespace AppliedCryptography
{
	ASN1Bool ASN1Bool::True(true);
	ASN1Bool ASN1Bool::False(false);

	ASN1Bool::ASN1Bool(ClassBits classBits, ConstructedBit constucted, TypeNumberBits number)
		: ASN1Encodable(classBits, constucted, number),
		_value(false)
	{
	};

	ASN1Bool::ASN1Bool(ClassBits classBits, ConstructedBit constucted, TypeNumberBits number, bool value)
		: ASN1Encodable(classBits, constucted, number),
		_value(value)
	{
	};

	ASN1Bool::ASN1Bool()
		: ASN1Encodable(Universal, Primitive, Boolean),
		_value(false)
	{
	};

	ASN1Bool::ASN1Bool(bool value)
		: ASN1Encodable(Universal, Primitive, Boolean),
		_value(value)
	{
	};

	ASN1Bool& ASN1Bool::set(bool value)
	{
		_value = value;
		return *this;
	};

	ASN1Bool& ASN1Bool::operator=(bool value)
	{
		this->set(value);
		return *this;
	};

	bool ASN1Bool::get() const
	{
		return _value;
	};

	std::vector<byte> ASN1Bool::getContentBits() const
	{
		std::vector<byte> buffer;

		buffer.push_back(_value ? 0xFF : 0x00);

		return buffer;
	};

	ASN1Bool ASN1Bool::getInstance(ASN1Encodable& obj)
	{
		ASN1Bool boolean;

		if(obj.getTypeBits() == boolean.getTypeBits())
		{
			return *(ASN1Bool*)&obj;
		};

		throw std::exception("Unknow object in getInstance()!");
	};
}; // namespace AppliedCryptography
